<template>
	<view class="page-info">
		<view class="btn-info-order">
			<view class="tips-border">
				<!-- <view class="tips">
					“客官，请留步”
				</view> -->
				<view class="tips">
					支付成功
				</view>
			</view>
			<view class="bio" @click="goIndex">
				<text style="line-height: 18px;">返回首页</text>
				<!-- <image src="/static/image/vd.png" mode="widthFix" style="width: 26px;margin-left:7px;"></image> -->
			</view>
		</view>

		<ad unit-id="adunit-0daff0d411198872" ad-type="video" ad-theme="white"></ad>

		<!-- <view class="list-title">
			附近好店
		</view> -->
		<!-- <div class="goods-list">
			<div v-for="(item,index) in goodsList" @click="goGoods(item)" :key="index">
				<div class="goods-item">
					<easy-loadimage v-if="item.storeHeadImage" loading-mode="spin-circle" mode="widthFix"
						:scroll-top="scrollTop" :image-src="picServiceUrl+item.storeHeadImage"></easy-loadimage>
					<div class="goods-flex">
						<div class="left">
							<div class="name">{{item.storeName}}</div>
							<div class="juli" v-show="item">
								{{item.subjectType}}
								<span v-if="item.distanceTencentKm">{{item.distanceTencentKm}}km</span>
							</div>
						</div>
					</div>
					<div class="label-info"
						v-if="item.businessAlipayMchid&&item.businessWechatMchid&&item.memberDiscount!=1">支持再花卡</div>
				</div> -->
		<!-- #ifdef MP-WEIXIN -->
		<!-- <ad-custom unit-id="adunit-818cd3d3bf3b880d" v-if="(index+1)%3==0"></ad-custom> -->
		<!-- #endif -->
		<!-- </div> -->
		<!-- </div> -->
		<view class="f-no-more" style="margin-bottom: 100px;" v-if="!busMore">
			没有更多了~
		</view>
	</view>
</template>

<script>
	let videoAd = null
	// 在页面中定义插屏广告
	let interstitialAd = null
	const app = getApp();
	import easyLoadimage from '@/components/easy-loadimage/easy-loadimage.vue'
	export default {
		components: {
			easyLoadimage
		},
		data() {
			return {
				userKey: null,
				busPage: 1,
				location: null,
				goodsList: [],
				busMore: true,
				scrollTop: 0,
				picServiceUrl: app.globalData.picServiceUrl,
			}
		},
		onLoad(e) {
			this.userKey = e.key;

			if (wx.createRewardedVideoAd) {
				videoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-196b69f8524a25a4'
				})
				videoAd.onLoad(() => {})
				videoAd.onError((err) => {})
				videoAd.onClose((res) => {
					// 用户点击了【关闭广告】按钮
					if (res && res.isEnded) {
						// 正常播放结束，可以下发游戏奖励
						uni.request({
							url: app.globalData.serviceUrl +
								'/UserMiniProgramController/getAdPrice',

							method: 'POST',
							data: {
								key: this.userKey
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							success: res => {
								var data = res.data;
								if (data.state == 1) {
									uni.showToast({
										title: "红包到手啦，去逛逛",
										icon: "none"
									})
									setTimeout(() => {
										this.goIndex();
									}, 1500)

								} else {
									uni.showToast({
										title: res.data.notice,
										icon: "none"
									})
								}
							}
						});

					} else {
						// uni.showToast({
						// 	title: "看完广告才能连接哦~",
						// 	icon: "none"
						// })
					}
				})
			}

			// 在页面onLoad回调事件中创建插屏广告实例
			if (wx.createInterstitialAd) {
				interstitialAd = wx.createInterstitialAd({
					adUnitId: 'adunit-c8481d90dddb3264'
				})
				interstitialAd.onLoad(() => {})
				interstitialAd.onError((err) => {})
				interstitialAd.onClose(() => {})
			}

			if (interstitialAd) {
				setTimeout(() => {
					interstitialAd.show().catch((err) => {
						console.error(err)
					})
				}, 2000)
			}
			this.getGps();
			this.getOfflineBusinessInfoList();
		},
		methods: {
			goIndex() {
				uni.switchTab({
					url: "/pages/newIndex/newIndex"
				})
				// uni.redirectTo({
				// 	url: "/pages/newIndex/newIndex"
				// })
			},
			goOrder() {
				if (videoAd) {
					videoAd.show().catch(() => {
						// 失败重试
						videoAd.load()
							.then(() => videoAd.show())
							.catch(err => {
								console.log('激励视频 广告显示失败')
							})
					})
				}

			},
			getGps() {
				var that = this;
				uni.getLocation({
					type: 'gcj02',
					altitude: true,

					success(res) {
						that.location = res;
						that.getOfflineBusinessInfoList();
					},

					fail(ff) {
						//用户拒绝授权
						uni.getSetting({
							success: function(res) {
								if (!res.authSetting['scope.userLocation']) {
									uni.showModal({
										title: '提示',
										content: '拒绝授权位置信息，将无法获取您附近商家信息',
										confirmText: '去设置',

										success(res) {
											if (res.confirm) {
												uni.openSetting({
													success(res) {
														var info = res.authSetting;
														console.log(res)
														if (res.authSetting[
																'scope.userLocation'
															]) {
															that.getGps();
														}
													}
												});
											} else {
												if (res.cancel) {
													that.getOfflineBusinessInfoList();
												}
											}
										}
									});
								}
							}
						});
						uni.setStorage({
							key: 'location',
							data: {
								latitude: 0,
								//写入默认0，后台判断为空
								longitude: 0
							}
						});
					}
				});
			},
			getOfflineBusinessInfoList() {
				var that = this;
				uni.request({
					url: app.globalData.serviceUrl +
						'/UserMiniProgramController/getOfflineBusinessInfoList',
					method: 'POST',
					data: {
						order_by: 1, // - 查询顺序 0 创建时间(默认) 1 经纬度距离
						order_by_clause: 0, // - 查询顺序方式 0 正序(默认) 1 倒序
						location_type: 1, // - 经纬度类型 0 高德(默认) 1 腾讯
						longitude: this.location ? this.location.longitude : "", // - 经度
						latitude: this.location ? this.location.latitude : "", // - 纬度
						type: 1, // - 类型 0 全部(默认) 1 正常 2 自营 3 自营不展示
						state: 2, // - 状态 0 全部 1 取消 / 删除 2 正常(默认)
						page: this.busPage, // - 当前页（ 可不传 默认第1页）
						page_size: 10, // - 页大小（ 可不传 默认20）
						keyword: '',
						recommend: 1
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						let info = res.data.data;
						if (info && info.length) {
							if (this.busPage == 1) {
								this.goodsList = info
							} else {
								this.goodsList = this.goodsList.concat(info)
							}
						} else {
							this.busMore = false;
						}
					}
				});
			},
			goGoods(item) {
				uni.navigateTo({
					url: "/pages/newIndex/goodsDetail/goodsDetail?item=" + JSON.stringify(item)
				});
			},
			goOrderByid(id) {
				uni.navigateTo({
					url: "/pages/qrCode/qrCode?id=" + id
				})
			},
			onPageScroll(e) {
				this.scrollTop = parseInt(e.scrollTop)
			},
			onReachBottom(e) {
				if (this.busMore) {
					this.setData({
						busPage: this.busPage + 1
					});
					this.getOfflineBusinessInfoList();
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.btn-info-order {
		border-top: 1px solid #eee;
		padding-top: 20px;

		.tips-border {
			border: 1px dashed #333;
			width: 230px;
			padding: 20px 0;
			margin: 0 auto;
		}

		.tips {
			text-align: center;
			font-size: 18px;
			color: #666;
			font-weight: bold;
		}

		.bio {
			width: 200px;
			height: 40px;
			font-size: 18px;
			background: #333;
			color: #fff;
			border-radius: 5px;
			box-shadow: 7px 6px 28px 1px rgba(0, 0, 0, 0.24);
			margin: 20px auto;
			display: flex;
			align-items: center;
			justify-content: center;

			&:active {
				box-shadow: 7px 6px 28px 1px rgba(0, 0, 0, 0.24);
				transform: translateY(4px);
			}
		}
	}

	.page-info {
		min-height: 100vh;
		width: 100%;
		background: rgba(242, 243, 248, 1);
	}

	.list-title {
		font-weight: bold;
		margin: 10px 10px 0;
		padding-left: 5px;
		border-left: 2px solid #333
	}


	.goods-list {
		padding: 10px;

		.goods-item {
			background: #fff;

			margin: 15px 0;
			border-radius: 10px;
			position: relative;

			::v-deep .loading-img {
				height: 280px;
			}

			::v-deep .easy-loadimage {
				width: 100%;
				border-radius: 10px;

			}

			.label-info {
				position: absolute;
				right: 0;
				top: 0;
				z-index: 99;
				color: #fff;
				background: #222;
				padding: 3px 5px;
				font-size: 12px;
				border-bottom-left-radius: 5px;
			}

			.goods-flex {
				display: flex;
				align-items: flex-end;
				padding: 10px;

				.right {
					flex: 0 0 70px;

					.btn {
						height: 30px;
						line-height: 30px;
						text-align: center;
						border: 1px solid #333;
						border-radius: 5px;
						color: #fff;
						background: #333;
						font-size: 14px;
					}
				}

				.left {
					flex: 1 1 0;
					padding-left: 10px;

					.name {
						font-size: 14px;
						margin: 5px 0;
					}

					.address {
						font-size: 12px;
						color: #666;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}

					.juli {
						font-size: 12px;
						color: #666;

						span {
							margin-left: 15px;
						}
					}
				}
			}


		}
	}
</style>